<?
require_once(dirname(__FILE__) . '/../../classes/module_interface.inc.php');
require_once(dirname(__FILE__) . '/../../classes/strings.inc.php');
require_once(dirname(__FILE__) . '/../../classes/browscap.inc.php');
require_once(dirname(__FILE__) . '/config.inc.php');

class LoggerModuleBrowsers extends LoggerStrings implements LoggerModuleInterface
{
    protected $database;
    protected $uaParser;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
        $this->stringsTable = 'module_browsers_strings';
        $this->uaParser = new LoggerBrowscap();
    }

    public function PrepareParseSession()
    {
    }

    public function ParseHit(&$hit)
    {
        if (!isset($hit['modules']['popularity']))
            return false;

        $arBrowser = $this->uaParser->getBrowser($hit['source']['agent']);
        if (!is_array($arBrowser))
            return false;

        $browserId  = $this->GetStringId($arBrowser['name'], $hit['source']['time']);
        $verId      = $this->GetStringId($arBrowser['ver'], $hit['source']['time']);

        $date       = '\'' . mysql_escape_string($hit['source']['time']) . '\'';
        $log        = intval($hit['modules']['popularity']['id']);
        $browser    = intval($browserId);
        $ver        = intval($verId);

        $InsertSQL = 'INSERT INTO `[PREF]module_browsers` (`date`, `log`, `browser`, `ver`) ';
        $InsertSQL .= 'VALUES('.$date.', '.$log.', '.$browser.', '.$ver.')';
        $result = $this->database->Query($InsertSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        return true;
    }

    public function Archive()
    {
        $date = LoggerModuleBrowsersConfig::GetArchiveDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $this->database->Query('LOCK TABLES `[PREF]module_browsers` WRITE, `[PREF]module_browsers_archive` WRITE');
        
        $ArchiveSQL = 'INSERT INTO `[PREF]module_browsers_archive` (`date`, `browser`, `ver`, `count`) ';
        $ArchiveSQL .= 'SELECT `date`, `browser`, `ver`, COUNT(*) FROM `[PREF]module_browsers` WHERE `date`<'.$dateEscaped.' ';
        $ArchiveSQL .= 'GROUP BY `date`, `browser`, `ver`';
        $this->database->Query($ArchiveSQL);
        $DeleteSQL = 'DELETE FROM `[PREF]module_browsers` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);

        $this->database->Query('UNLOCK TABLES');
    }

    public function Purge()
    {
        $date = LoggerModuleBrowsersConfig::GetPurgeDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $DeleteSQL = 'DELETE FROM `[PREF]module_browsers` WHERE `time`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);
        $DeleteSQL = 'DELETE FROM `[PREF]module_browsers_archive` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_browsers`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_browsers_archive`';
        $this->database->Query($OptimizeSQL);

        $this->PurgeStrings($date);

        return true;
    }
}